package cn.msqweb.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.msqweb.entity.Prod;
import cn.msqweb.entity.ProdComm;
import cn.msqweb.model.Result;
import cn.msqweb.service.ProdService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商品接口管理")
@RequestMapping("prod/prod")
@RestController
public class ProdController {

    @Resource
    private ProdService prodService;

    @ApiOperation("多条件分页查询")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:prod:page')")
    public Result<Page<Prod>> loadProdPage(@ApiParam("页码") @RequestParam Long current,
                                           @ApiParam("每页显示条数") @RequestParam Long size,
                                           @ApiParam("商品状态") @RequestParam(required = false) Integer status,
                                           @ApiParam("商品名称") @RequestParam(required = false) String prodName){

        Page<Prod> prodPage = new Page<>(current,size);
        Page<Prod> page = prodService.page(prodPage, new LambdaQueryWrapper<Prod>()
                .eq(ObjectUtil.isNotNull(status), Prod::getStatus, status)
                .like(StringUtils.hasText(prodName), Prod::getProdName, prodName));

        return Result.success(page);
    }

    @ApiOperation("新增商品")
    @PostMapping
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public Result<String> saveProd(@ApiParam("商品对象") @RequestBody Prod prod) {
        Boolean saved = prodService.saveProd(prod);
        return Result.handler(saved);
    }
    @ApiOperation("根据标识查询商品详情")
    @GetMapping("info/{prodId}")
    @PreAuthorize("hasAuthority('prod:prod:info')")
    public Result<Prod> loadProdDetailInfo(@ApiParam("商品标识") @PathVariable Long prodId) {
        Prod prod = prodService.queryProdDetailInfoByProdId(prodId);
        return Result.success(prod);
    }

    @ApiOperation("编辑商品信息")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:prod:update')")
    public Result<String> modifyProd(@ApiParam("商品对象") @RequestBody Prod prod) {
        Boolean modified = prodService.modifyProd(prod);
        return Result.handler(modified);
    }

    @ApiOperation("批量删除商品")
    @DeleteMapping("{prodIds}")
    @PreAuthorize("hasAuthority('prod:prod:delete')")
    public Result<String> removeProdList(@ApiParam("商品id集合") @PathVariable List<Long> prodIds) {
        Boolean removed = prodService.removeProdListByIds(prodIds);
        return Result.handler(removed);
    }

    /**
     * feign接口
     * @param ids
     * @return
     */
    @ApiOperation("根据商品id集合查询商品对象集合")
    @GetMapping("getProdListByIds")
    public Result<List<Prod>> getProdListByIds(@RequestParam List<Long> ids) {
        List<Prod> prods = prodService.listByIds(ids);
        return Result.success(prods);
    }
}
